1 Description
This is a compilation of five homework assignments completed in Spring 2022 for ESS 580: Ecological Data Science.
<!--chapter:end:index.Rmd-->
```r
library(tidyverse)
library(dataRetrieval)
library(dygraphs)
library(xts)
library(yaml)
2 1: Discharge Example
2.1 Methods
The Poudre River at Lincoln Bridge is:
Downstream of only a little bit of urban stormwater
Near Odell Brewing CO
Near an open space area and the Poudre River Trail
Downstream of many agricultural diversions
2.2 Site Description

2.3 Data Acquisition and Plotting tests
2.4 Data Download
2.5 Static Data Plotter
ggplot(q, aes(x = Date, y = q)) +
geom_line() +
ylab('Q (cfs)') +
ggtitle('Discharge in the Poudre River, Fort Collins')
2.7 DyGraph example.
q_xts <- xts(q$q, order.by = q$Date)
dygraph(q_xts) %>%
dyAxis("y", label = "Discharge (cfs)") %>% dyEvent("2017-5-27", "27 May", labelLoc = "top") %>% dyEvent("2018-5-28", "28 May", labelLoc = "top")%>% dyEvent("2019-6-09", "09 Jun", labelLoc = "top")%>% dyEvent("2020-6-01", "01 Jun", labelLoc = "top")%>% dyEvent("2021-5-23", "23 May", labelLoc = "top")2.8 Poudre Paragraph
The Cache la Poudre, or “Poudre” River is located in the Front Range of Colorado’s Rocky Mountains. The name comes from French trappers who hid their gunpowder near the river. The Arapahoe name for the river is ho’oowu’ heetou’ , which means ‘where a house is located,’ according to CU Boulder’s Center for the Study of Indigenous Languages of the West. The Poudre River is a tributarty of the South Platte, which later joins the Platte River, which is a tributary of the Missouri River, which in turn feeds the Mississippi River and flows into the Gulf of Mexico. According to the Coalition for the Poudre River Watershed, the Poudre supports water supply for over 330,000 residents and 151,547 acres of irrigated land.
3 3: Webscraping and Iterations
3.1 Assignment:
- Extract the meteorological data URLs. Here we want you to use the
rvestpackage to get the URLs for theSASP forcingandSBSP_forcingmeteorological datasets.c
## Warning in dir.create(datapath): 'data' already exists
site_url <- 'https://snowstudies.org/archived-data/'
#Read the web url
webpage <- read_html(site_url)
links <- webpage %>%
html_nodes('a') %>%
.[grepl('forcing',.)] %>%
html_attr('href')
#Grab only the name of the file by splitting out on forward slashes
splits <- str_split_fixed(links,'/',8)
#Keep only the 8th column
dataset <- splits[,8]
# view(dataset)
file_names <- paste0(datapath,dataset)- Download the meteorological data. Use the
download_fileandstr_split_fixedcommands to download the data and save it in your data folder. You can use a for loop or a map function.
#generate a file list for where the data goes
file_names <- paste0('data/',dataset)
for(i in 1:2){
download.file(links[i],destfile=file_names[i])
}
downloaded <- file.exists(file_names)
evaluate <- !all(downloaded)- Write a custom function to read in the data and append a site column to the data.
# this code grabs the variable names from the metadata pdf file
library(pdftools)
headers <- pdf_text('https://snowstudies.org/wp-content/uploads/2022/02/Serially-Complete-Metadata-text08.pdf') %>%
readr::read_lines(.) %>%
trimws(.) %>%
str_split_fixed(.,'\\.',2) %>%
.[,2] %>%
.[1:26] %>%
str_trim(side = "left")read_in_weatherdata <- function(file){
name = str_split_fixed(file,'_',3)[,2]
df <- read.delim(file, header = F, sep = "", skip =4) %>% mutate(site = name)
return(df)
}- Use the
mapfunction to read in both meteorological files. Display a summary of your tibble.
setwd("~/Spring 2022/ESS580/3_snow_functions_iteration/data")
weather_data_full <- map_dfr(dataset, read_in_weatherdata)
weather_data_full <- select(weather_data_full, V1, V2, V10, site)%>% rename(Year = V1, Month = V2, temp = V10)
summary(weather_data_full)## Year Month temp site
## Min. :2003 Min. : 1.000 Min. :242.1 Length:138336
## 1st Qu.:2005 1st Qu.: 3.000 1st Qu.:265.8 Class :character
## Median :2007 Median : 6.000 Median :272.6 Mode :character
## Mean :2007 Mean : 6.472 Mean :272.6
## 3rd Qu.:2009 3rd Qu.: 9.000 3rd Qu.:279.7
## Max. :2011 Max. :12.000 Max. :295.8
- Make a line plot of mean temp by year by site (using the
air temp [K]variable). Is there anything suspicious in the plot? Adjust your filtering if needed.
weather_data_yearlymean <- weather_data_full %>% group_by(Year,site) %>% filter(Year != 2003, Year != 2011) %>% summarize(meantemp = mean(temp))ggplot(weather_data_yearlymean, aes(x = Year, y = meantemp, color = site )) +
geom_line() +
labs( x = "Year", y = "Mean Temperature (K)")
The dataset only included parts of 2003 and 2011, which skewed the yearly average for both years.
- Write a function that makes line plots of monthly average temperature at each site for a given year. Use a for loop to make these plots for 2005 to 2010. Are monthly average temperatures at the Senator Beck Study Plot ever warmer than the Snow Angel Study Plot? Hint: https://ggplot2.tidyverse.org/reference/print.ggplot.html
# create a function
yearly_plotter <- function(df, year){
monthly_mean <- df %>%
group_by(Year, Month, site) %>%
summarize(meantemp = mean(temp)) %>% filter(year == Year)
figure <- ggplot(monthly_mean, aes(x = Month, y = meantemp, color = site )) +
geom_line() +
labs( x = "Month", y = "Mean Temperature (K)")
print(figure)
}# run the function in a for loop
x <- c(2005, 2006, 2007, 2008, 2009, 2010)
for(year in x){(yearly_plotter(weather_data_full, year))}





4 4: LAGOS Analysis
4.1 Loading in data
4.1.1 First download and then specifically grab the locus (or site lat longs)
4.1.2 Convert to spatial data
#Look at the column names
#names(lake_centers)
#Look at the structure
#str(lake_centers)
#View the full dataset
#View(lake_centers %>% slice(1:100))
spatial_lakes <- st_as_sf(lake_centers,coords=c('nhd_long','nhd_lat'),
crs=4326) %>%
st_transform(2163)
#Subset for plotting
subset_spatial <- spatial_lakes %>%
slice(1:100)
subset_baser <- spatial_lakes[1:100,]
#Dynamic mapviewer
mapview(subset_spatial)4.1.3 Subset to only Minnesota
states <- us_states()
#Plot all the states to check if they loaded
#mapview(states)
minnesota <- states %>%
filter(name == 'Minnesota') %>%
st_transform(2163)
#Subset lakes based on spatial position
minnesota_lakes <- spatial_lakes[minnesota,]
#Plotting the first 1000 lakes
minnesota_lakes %>%
arrange(-lake_area_ha) %>%
slice(1:1000) %>%
mapview(.,zcol = 'lake_area_ha')4.2 1) Show a map outline of Iowa and Illinois (similar to Minnesota map upstream)
4.3 2) Subset LAGOS data to these sites, how many sites are in Illinois and Iowa combined? How does this compare to Minnesota?
## Simple feature collection with 1 feature and 1 field
## Geometry type: MULTIPOINT
## Dimension: XY
## Bounding box: xmin: 277299.3 ymin: -824038.9 xmax: 1080254 ymax: -128993.5
## Projected CRS: NAD27 / US National Atlas Equal Area
## n geometry
## 1 16466 MULTIPOINT ((277299.3 -2468...
There are 16466 sites in Iowa and Illinois combined, which is a little more that half the sites in Minnesota alone.
4.4 3) What is the distribution of lake size in Iowa vs. Minnesota?
- Here I want to see a histogram plot with lake size on x-axis and frequency on y axis (check out geom_histogram)
Q3 <- spatial_lakes %>% filter(state_zoneid == "State_14" | state_zoneid == "State_13") %>%
mutate(state = ifelse(state_zoneid == "State_14", paste("Minnesota"), paste("Iowa")))
ggplot(Q3, aes(lake_area_ha))+
geom_histogram(bins = 4) +
# scale_x_continuous(breaks = seq(0, 130000, 4))
facet_wrap(~state) 
## [1] 123779.8
The majority of lakes in both states are under 25,000 acres. Minnesota has many more lakes.